<?php

class EventReceiver
{
	private $taskService;
	private $taskId;
	private $taskResult;
	
	public function __construct($taskId) {
		$this->taskService = TaskService::getInstance();
		$this->taskId = $taskId;
	}
	
	public function getTaskResult() 
	{
		return $this->taskResult;
	}
	
    public function afterExecutionStarted( ezcWorkflowExecution $execution )
    {
    	$this->taskResult = array();
    }

    public function afterExecutionSuspended( ezcWorkflowExecution $execution )
    {
    }

    public function afterExecutionResumed( ezcWorkflowExecution $execution )
    {
    }

    public function afterExecutionCancelled( ezcWorkflowExecution $execution )
    {
    }

    public function afterExecutionEnded( ezcWorkflowExecution $execution )
    {
        //$this->taskService->editTaskAttributes($this->taskId, array('result_info' =>  CJSON::encode($this->taskResult)));
    }

    public function beforeNodeActivated( ezcWorkflowExecution $execution, ezcWorkflowNode $node, ezcWorkflowSignalSlotReturnValue $return )
    {
    }

    public function afterNodeActivated( ezcWorkflowExecution $execution, ezcWorkflowNode $node )
    {
    }

    public function afterNodeExecuted( ezcWorkflowExecution $execution, ezcWorkflowNode $node )
    {
		if($node instanceof ezcWorkflowNodeAction) {
			$serviceObject = $node->getObject();
			$actionResult = array(
            	'cmd'	=>  $serviceObject->getCmd(),
                'size'	=>	$serviceObject->getResultSize(),
                'count'	=>	$serviceObject->getResultCount(),
                'cost'	=>	$serviceObject->getCost(),
			);
			
            if($serviceObject instanceof HiveAction) {               
                $actionResult['hive'] = $serviceObject->getHiveQL();	
            }
            $this->taskResult[$serviceObject->getName()] = $actionResult;
		}
    }

    public function afterThreadStarted( ezcWorkflowExecution $execution, $threadId, $parentId, $numSiblings )
    {
    }

    public function afterThreadEnded( ezcWorkflowExecution $execution, $threadId )
    {
    }

    public function beforeVariableSet( ezcWorkflowExecution $execution, $variableName, $value, ezcWorkflowSignalSlotReturnValue $return )
    {
    }

    public function afterVariableSet( ezcWorkflowExecution $execution, $variableName, $value )
    {
    }

    public function beforeVariableUnset( ezcWorkflowExecution $execution, $variableName, ezcWorkflowSignalSlotReturnValue $return )
    {
    }

    public function afterVariableUnset( ezcWorkflowExecution $execution, $variableName )
    {
    }
}
